
class MeiyidaSearch extends AbstractPlugin {

  static BASE_URL = 'https://www.mydys1.com';

  async fetchSearchResults(query) {
    const searchUrl = `${MeiyidaSearch.BASE_URL}/vodsearch/-------------.html?wd=${encodeURIComponent(query)}`;
    const http_headers = {
      'User-Agent': MeiyidaSearch.DEFAULT_USER_AGENT,
      'Origin': MeiyidaSearch.BASE_URL,
    };
    const result  = await doHttpRequestSync(searchUrl, { headers: http_headers });
    if(result.status !== 'success') {
      return null;
    }
    const data = result.body;

    const parser = new DOMParser();
    const doc = parser.parseFromString(data, 'text/html');

    const searchResults = [];

    const results = doc.querySelectorAll('.module-card-item.module-item');
    results.forEach(result => {
      const searchResult = new SearchResult();
      searchResult.site_icon = '/plugins/meiyida/icon.png';
      searchResult.site_name = MeiyidaSearch.BASE_URL.replace(/https?:\/\/(www\.)?/, '');

      const imageElement = result.querySelector('.module-item-pic img');
      searchResult.image = imageElement ? imageElement.getAttribute("data-original").trim() : '';

      const titleElement = result.querySelector('.module-card-item-title a');
      searchResult.title = titleElement ? titleElement.textContent.trim() : '';

      const href = titleElement ? titleElement.getAttribute('href') : '';
      searchResult.link = href.startsWith('/') ? `${MeiyidaSearch.BASE_URL}${href}` : href;

      const infos = [];
      result.querySelectorAll('.module-info-item-content').forEach(infoElement => {
        infos.push(infoElement.textContent.trim());
      });
      searchResult.description = infos.join('\n');

      searchResults.push(searchResult);
    });

    return searchResults;
  }

}
// Registry.registerClass("MeiyidaSearch", new MeiyidaSearch());